package net.mymilkedeek.ludum.tinythumb.states;

import net.mymilkedeek.ludum.tinythumb.Game;
import net.mymilkedeek.ludum.tinythumb.Heal;
import net.mymilkedeek.ludum.tinythumb.model.Battaljar;
import net.mymilkedeek.ludum.tinythumb.model.PartyFactory;
import net.mymilkedeek.ludum.tinythumb.util.Constants;
import net.mymilkedeek.ludum.tinythumb.util.URL;
import org.newdawn.slick.*;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.Log;

import java.util.List;

/**
 * TODO: Write Documentation
 *
 * @author Michael Demey
 */
public class BattleState extends BasicGameState {
    @Override
    public int getID() {
        return Game.S_BATTL;
    }
    private Sound nope;
    private Sound healfx;
    private List<String> log;
    private Animation thumb;
    private Image chicken;
    private Image background;
    private Image enemy1;
    private Image enemy2;
    private int thumbx;
    private int thumby;
    private int chickenx;
    private int chickeny;

    private int enemy1x;
    private int enemy1y;
    private int enemy2x;
    private int enemy2y;

    private int arrowx;
    private int arrowy;

    private int choice = 1;

    private int[] attack;
    private int current = 0;

    private boolean battleOver;
    private boolean gameOver;

    private int defaultAx;
    private int defaultAy;

    public void setGameOver(boolean gameOver) {
        this.gameOver = gameOver;
    }

    public void setBattleOver(boolean battleOver) {
        this.battleOver = battleOver;
    }

    @Override
    public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException {
        Log.debug("BATTGAME   - Initializing");
        thumb = new Animation(new SpriteSheet(URL.character_sprites_down, Constants.tile_size, Constants.tile_size), Constants.animation_character_refresh_speed);
        chicken = new Image(URL.chicken_sprite);
        background = new Image(URL.battle);

        nope = new Sound(URL.sfx_nope);
        healfx = new Sound(URL.sfx_heal);

        thumbx = 50;
        thumby = 240;

        enemy1x = 300;
        enemy1y = 240;

        chickenx = 70;
        chickeny = 220;

        enemy2x = 260;
        enemy2y = 220;

        defaultAx = 260;
        defaultAy = 100;
    }

    public boolean initialized = false;

    @Override
    public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException {
        graphics.setColor(Color.white);
        if (Battaljar.victory) {
            Log.debug("victory");
            graphics.setColor(Color.black);

            new Image(URL.victory).draw(0, 0);
            graphics.drawString("+10 XP", 150, 250);
            graphics.drawString("+10 XP", 250, 250);

            if (showXp) {
                graphics.setColor(Color.white);
                graphics.drawString("Someone gained a level!", 100, 150);
            }
        } else {
            background.draw(0, 0);
            chicken.draw(chickenx, chickeny);
            thumb.draw(thumbx, thumby);

            graphics.drawString("hp " + Battaljar.party1.getMember1().hp, thumbx - 30, chickeny - 65);
            graphics.drawString("hp " + Battaljar.party1.getMember2().hp, chickenx + 20, chickeny - 65);

            MainGameState m = (MainGameState) stateBasedGame.getState(Game.S_MAIN);

            if (!m.boss1 && !m.boss2) {
                //if (!initialized) {
                    if (enemy1 == null && enemy2 == null) {
                        Battaljar.initParty2();
                        Log.debug(URL.enemies + Battaljar.party2.getMember1().name.toLowerCase() + ".png");
                        enemy1 = new Image(URL.enemies + Battaljar.party2.getMember1().name.toLowerCase() + ".png");
                        if (Battaljar.party2.getMember2() != null) {
                            enemy2 = new Image(URL.enemies + Battaljar.party2.getMember2().name.toLowerCase() + ".png");
                        }
                    }
                   // initialized = true;
                //}
            }

            if (m.boss1) {
                if (Battaljar.party2.getMember1() == null) {
                    Battaljar.party2 = PartyFactory.gnomeUp();
                }
                Log.debug("member1" + (Battaljar.party2.getMember1() == null));
                Log.debug("member1" + (Battaljar.party2.getMember1().name == null));
                Log.debug(Battaljar.party2.getMember1().name);
                enemy1 = new Image(URL.enemies + Battaljar.party2.getMember1().name.toLowerCase() + ".png");
            } else if (Battaljar.party2.getMember1().name.equalsIgnoreCase("foot")) {
                enemy1 = new Image(URL.enemies + Battaljar.party2.getMember1().name.toLowerCase() + ".png");
            }

            if (log != null) {
                graphics.setColor(Color.black);

                graphics.fillRect(129, 35, 100, 30);

                graphics.setColor(Color.white);

                graphics.drawString("Turn #" + Battaljar.turn, 139, 40);
            }

            if (choice == 1) {
                arrowx = enemy1x + 20;
                arrowy = enemy1y - 40;
            } else {
                arrowx = enemy2x + 20;
                arrowy = enemy2y - 20;
            }

            graphics.drawString("V", arrowx, arrowy);

            if (Battaljar.party2.getMember1().name.equalsIgnoreCase("gnome") || Battaljar.party2.getMember1().name.equalsIgnoreCase("foot")) {
                enemy1.draw(defaultAx, defaultAy);
            } else {
                Log.debug("who am i");
                if (enemy2 != null) {
                    enemy2.draw(enemy2x, enemy2y);
                    graphics.drawString("hp " + Battaljar.party2.getMember2().hp, enemy2x - 20, enemy2y - 60);
                }

                if (enemy1 != null) {
                    enemy1.draw(enemy1x, enemy1y);
                    graphics.drawString("hp " + Battaljar.party2.getMember1().hp, enemy1x, enemy1y - 80);
                }
            }

        }
    }

    private boolean showXp = false;
    private boolean pressed = false;
    private boolean xpAdded = false;

    @Override
    public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int i) throws SlickException {
        Input input = gameContainer.getInput();

        if (Battaljar.party1.getMember1().hp <= 0 && Battaljar.party1.getMember2().hp <= 0 && gameOver) {
            Battaljar.end();
            MainGameState m = (MainGameState) stateBasedGame.getState(Game.S_MAIN);
            m.reset(gameContainer, stateBasedGame);
            stateBasedGame.enterState(Game.S_INTRO);
        }

        if (showXp || xpAdded) {
            if (!pressed) {
                if (input.isKeyPressed(Input.KEY_SPACE)) {
                    showXp = false;
                    pressed = true;
                    battleOver = true;
                    xpAdded = false;
                }
            } else {
                showXp = true;
            }
        }
        
        if (Battaljar.victory) {
            MainGameState m = (MainGameState) stateBasedGame.getState(Game.S_MAIN);
            Log.debug("vicotry bb");
            if ( m.boss2 ) {
                Log.debug("m2boss in ze battlestate");
                stateBasedGame.enterState(Game.S_END);
            }
        }

        if (Battaljar.victory && !battleOver && !showXp && !pressed) {
            int lv1 = Battaljar.party1.getMember1().lvl;
            int lv2 = Battaljar.party1.getMember2().lvl;
            if (!xpAdded) {
                Log.debug("add xp");
                Battaljar.party1.addXp();
                xpAdded = true;
            }

            if (lv1 != Battaljar.party1.getMember1().lvl || Battaljar.party1.getMember2().lvl != lv2) {
                showXp = true;
            }
        }

        MainGameState m = (MainGameState) stateBasedGame.getState(Game.S_MAIN);
        if (Battaljar.victory && battleOver && !m.boss2) {
            Log.debug("going back to main");
            Battaljar.end();
            Battaljar.initParty2();
            showXp = false;
            pressed = false;
            m.boss1 = false;
            m.boss2 = false;
            stateBasedGame.enterState(Game.S_MAIN);
        }

        if (input.isKeyPressed(Input.KEY_RIGHT) || input.isKeyPressed(Input.KEY_LEFT)) {
            if (!Battaljar.victory) {
                if (enemy1 == null) {
                    choice = 2;
                } else if (enemy2 == null) {
                    choice = 1;
                } else {
                    choice = choice == 1 ? 2 : 1;
                }
            }
        }

        if ( input.isKeyPressed(Input.KEY_H)) {
            if ( Heal.uses > 0  ) {
                Battaljar.party1.getMember1().hp += 100;
                Battaljar.party1.getMember2().hp += 100;
                Heal.uses--;
                healfx.play();
            } else {
                nope.play();
            }
        }

        if (input.isKeyPressed(Input.KEY_SPACE)) {
            if (m.boss1 || m.boss2) {
                log = Battaljar.attack(1, 1);
                if (Battaljar.party2.getMember1().hp <= 0) {
                    Log.debug("is nullllll");
                    enemy1 = null;
                    Battaljar.party2 = null;
                    Battaljar.initParty2();
                    Battaljar.victory = true;
                }
            } else {
                if (current == 0) {
                    attack = new int[2];
                }

                if (current <= 1) {
                    attack[current] = choice;
                    current++;
                }

                if (current == 2) {
                    log = Battaljar.attack(attack[0], attack[1]);
                    if (enemy2 != null) {
                        if (Battaljar.party2.getMember2().hp <= 0) {
                            enemy2 = null;
                        }
                    }
                    if (enemy1 != null) {
                        Log.debug("battlestate check " + (Battaljar.party2.getMember1().hp <= 0));
                        if (Battaljar.party2.getMember1().hp <= 0) {
                            enemy1 = null;
                        }
                    }

                    if (enemy1 == null && enemy2 == null) {
                        Battaljar.victory = true;
                    }
                    current = 0;
                }
            }
        }


        if (input.isKeyPressed(Input.KEY_F3)) {
            if (!m.boss1 && !m.boss2) {
                Battaljar.end();
                stateBasedGame.enterState(Game.S_MAIN);
            }
        }
    }
} 